﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;

namespace BPAssistantWebApp.DBConnect
{
    public class DBHelp
    {

        public string MessageBox(string TxtMessage)
        {
            string str;
            str = "<script language=javascript>alert('" + TxtMessage + "')</script>";
            return str;
        }

        ///<Summary>
        ///连接数据库
        ///</Summary>
        ///<returns>返回SqlConnection对象</returns>
        ///

        public SqlConnection GetConn()
        {
            return new SqlConnection(ConfigurationManager.ConnectionStrings["BP_Help"].ConnectionString);
        }

        ///<Summary>
        ///执行SQL 语句
        ///</Summary>
        ///<param name = "cmdstr">执行语句</param>
        ///<returns>返回值为int型，成功返回1，失败返回0</returns>

        public Boolean ExecuteSql(string sqlString)
        {
            SqlConnection conn = GetConn();
            conn.Open();
            SqlCommand cmd = new SqlCommand(sqlString, conn);
            try
            {
                if (cmd.ExecuteNonQuery() > 0)
                {
                    conn.Close();
                    return true;   //成功返回
                }
                else
                {
                    conn.Close();
                    return false;
                }
            }
            catch (Exception e)
            {
                throw e;
            }
            finally
            {
                conn.Dispose();  //释放连接对象资源
            }
        }


        ///<Summary>
        ///执行SQL 查询语句
        ///</Summary>
        ///<param name = "cmdstr">查询语句</param>
        ///<returns>返回DataTable 数据表</returns>

        public DataSet GetDt(string sqlString, string tableName)
        {
            SqlConnection conn = GetConn();
            SqlDataAdapter sda = new SqlDataAdapter(sqlString, conn);
            DataSet ds = new DataSet();
            sda.Fill(ds, tableName);
            return ds;
        }

    }
}